package bookstore.dao;

import static java.lang.System.out;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

import bookstore.domain.MSG;

@SuppressWarnings("rawtypes")
public class CabinetImpl implements Cabinet{	 
	private ObjectOutputStream output;    
	private ObjectInputStream input;
	
	public boolean save(List list,String filename){
		boolean isOK = false; 		
			
		try{
			output = new ObjectOutputStream(new FileOutputStream(filename));
			output.writeObject(list);
			isOK = true;
		}catch(FileNotFoundException e){
			out.println(MSG.FAIL+e.getMessage());
		}catch(IOException e){
			out.println(MSG.FAIL+e.getMessage());
		}finally{
			try{output.close();}catch(Exception e){}
		}

		return isOK;
	}	
	
	public List load(String filename){
		List list = null;
		File file = new File(filename);
		if(file.exists()){
			try{
				input = new ObjectInputStream(new FileInputStream(file));
				list = (List)input.readObject();    		 
			}catch(IOException e){
				out.println(MSG.FAIL+e.getMessage());
			}catch(ClassNotFoundException e){
				out.println(MSG.FAIL+e.getMessage());
			}finally{
				try{input.close();}catch(Exception e){}
			}
		}
		
		return list;
	}

}
